package com.ysq.frame;

import java.awt.Point;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

/**
 * 测试类
 * @author Administrator
 *
 */
public class MyTest {
	Random random = new Random();
	
	int maxX = 6;
	int maxY = 6;
	
	Point snakeHead = new Point(2, 3);
	
	public Point createRandomPoint(List<Point> bodyList){
		//记录列号
		Set<Integer> colNumberSet = new HashSet<Integer>();
		//将所有的列号放入set
		for(int i = 0;i<maxX;i++){
			colNumberSet.add(i);
		}
		//列集合
		List<Set<Integer>> indexRowList = new ArrayList<Set<Integer>>();
		for(int i = 0;i<maxY;i++){
			Set<Integer> clonColSet = new HashSet<Integer>(colNumberSet);
			indexRowList.add(clonColSet);
		}
		//移除头所在的x坐标
		indexRowList.get(snakeHead.y).remove(snakeHead.x);
		
		//移除存在的坐标
		for(int i = 0;i<bodyList.size();i++){
			Point bodyPoint = bodyList.get(i);
			indexRowList.get(bodyPoint.y).remove(bodyPoint.x);
		}
		
		List<Integer> randomRowList = new ArrayList<Integer>();
		for(int i = 0;i<indexRowList.size();i++){
			if(!indexRowList.get(i).isEmpty()){
				randomRowList.add(i);
			}
		}
		int randomInt = random.nextInt(randomRowList.size());
		
		int randomX = randomRowList.get(randomInt);
		
		List<Integer> colList = new ArrayList<Integer>(indexRowList.get(randomX));
		
		randomInt = random.nextInt(colList.size());
		int randomY = colList.get(randomInt);
		
		Point randomPoint = new Point(randomX,randomY);
		return randomPoint;
	}
	
	public static void main(String[] args) {
		MyTest mt = new MyTest();
		List<Point> bodyList = new ArrayList<Point>();
		for(int i=0;i<6;i++){
			Point onepoint = new Point(i, 1);
			bodyList.add(onepoint);
		}
		
		mt.createRandomPoint(bodyList);
		System.out.println("success");
	}
	
	
}
